﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Windows.Forms;

namespace SiteMapMerging
{
    [Serializable]
    public class ConfigInfo
    {
        public List<string> Urls { set; get; }
        public string SaveFileName { set; get; }
        public string FilterFileName { set; get; }

        public ConfigInfo()
        {
            Urls = new List<string>();
            SaveFileName = string.Empty;
            FilterFileName = string.Empty;
        }
    }


    public class ConfigMgr
    {
        private static string Name = "SiteMapMerging.cfg";
        public static void Serialize(ConfigInfo parameterInfo)
        {
            string fileName = Path.Combine(Application.StartupPath, Name);
            using (FileStream fileStream = new FileStream(fileName, FileMode.Create))
            {
                BinaryFormatter b = new BinaryFormatter();
                b.Serialize(fileStream, parameterInfo);
                fileStream.Close();
            }
        }

        public static ConfigInfo DeSerialize()
        {
            ConfigInfo parameterInfo = null;
            string fileName = Path.Combine(Application.StartupPath, Name);
            if (!File.Exists(fileName)) return new ConfigInfo();
            try
            {
                using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    BinaryFormatter bf = new BinaryFormatter();
                    parameterInfo = bf.Deserialize(fileStream) as ConfigInfo;
                    fileStream.Close();
                }
            }
            catch
            {
                parameterInfo = new ConfigInfo();
            }
            return parameterInfo;
        }
    }
}
